<?php
namespace app\hsycms\controller;
use think\Controller;
use app\hsycms\controller\Base;

class Coupon extends Base
{
			//优惠券列表
			public function index(){	
						if(input('s_status')!=""){
									$map['status'] = input('s_status');
						}
						if(input('title')!=""){
								$map['title|type|price'] = array('like','%'.input('title').'%');
						}		
						$list = db('coupon')->where($map)->order("id desc,datetime desc")->paginate(10,false,['query' => request()->param()]);	
						$page = $list->render();					
						$this->assign("list",$list);
						$this->assign("page",$page);
						return $this->fetch(); 
		}
		
		//已领取优惠券列表
			public function receive(){	
						if(input('s_use')!=""){
									$map['use'] = input('s_use');
						}
						if(input('title')!=""){
								$map['coupontitle|couponprice|coupontype|couponday|memid|mobile'] = array('like','%'.input('title').'%');
						}
						//判断未使用的优惠券是否过期，如已过期设置use为2
						$yxlist = db('memcoupon')->where(['use'=>0])->select();
						if($yxlist){foreach($yxlist as $key=>$v){	checkCouponUse($v['id'],$v['expiretime']);}}						
						$list = db('memcoupon')->where($map)->order("id desc,receivetime desc")->paginate(10,false,['query' => request()->param()]);	
						$page = $list->render();					
						$this->assign("list",$list);
						$this->assign("page",$page);
						return $this->fetch(); 
		}
		
		//删除已领取
		public function delreceive(){
				$id  = input('id');		
				db('memcoupon')->where('id',$id)->delete();		
				return json(['status'=>'y','info'=>'删除成功']);				
		}
				
		//添加优惠券
		public function add(){
			 $id = input('id','','intval');
			 if($_POST){
					  $data = input('post.');
							if($id==0){
								 $data['datetime'] = date('Y-m-d H:i:s',time());
								 db('coupon')->insert($data);
									return json(['status'=>'y','info'=>'添加成功']);
							}else{
								 $res = db('coupon')->where(['id'=>$id])->update($data);
									if($res!==false){
										  return json(['status'=>'y','info'=>'修改成功']);
									}
							}							
				}else{
					  if($id!=0){
										$one = db('coupon')->where(['id'=>$id])->find();
										$this->assign("v",$one);
							}
					  return $this->fetch();
				}
		}
		
		//发放优惠券首页
		public function fafang(){
			 $couponid = input('id');				
				if(input('s_status')!=""){
						$map['status'] = input('s_status');
				}					
				if(input('title')!=""){			
						$map['mobile|name'] = array('like','%'.input('title').'%');
				}				
				$list = db('member')->where($map)->order($order)->paginate(10,false,['query' => request()->param()]);
				$page = $list->render();		
				$this->assign("couponid",$couponid);
				$this->assign("list",$list);
				return $this->fetch();	
		}
		
		//开始发送优惠券
		public function sendCoupon(){
			  $couponid = input('couponid');
					$coupon = db('coupon')->where(['id'=>$couponid])->find();
					if($coupon['status']==0 && $coupon['num']<=0){ $this->error("发放失败，优惠券数量不足或是关闭状态"); } 
					$receivetime = date('Y-m-d H:i:s',time());
					$expiretime  = date("Y-m-d H:i:s",strtotime("+".$coupon['day']." day"));
					if($_POST['id'])
					foreach($_POST['id'] as $v){	
							$data['memid'] = $v;
							$data['mobile'] = db('member')->where(['id'=>$v])->value('mobile');
							$data['couponprice'] = $coupon['price'];
							$data['coupontitle'] = $coupon['title'];
							$data['coupontype'] = $coupon['type'];
							$data['couponday'] = $coupon['day'];
							$data['couponid'] = $coupon['id'];
							$data['receivetime'] = $receivetime;
							$data['expiretime']  = $expiretime;
							db('memcoupon')->insert($data);
							db('coupon')->where(['id'=>$couponid])->setField(['num'=>$coupon["num"]-1]);
					}
					$this->success("发放成功",url('index'));		
		}
			
	
		//删除优惠券
		public function del(){	
					$id  = input('id');				
					db('coupon')->where('id',$id)->delete();		
					return json(['status'=>'y','info'=>'删除成功']);				
		}
	
		//指量删除优惠券
		public function delAll(){
			if($_POST['id'])foreach($_POST['id'] as $v){				
						db('coupon')->where(['id'=>$v])->delete();			
			}
			$this->success("批量删除成功",url('index'));
		}
		
	//批量处理状态	
	public function upStatus(){	
			$d['status'] = input('status');			
			if($_POST['id'])
			foreach($_POST['id'] as $v){	
					db('coupon')->where(['id'=>$v])->setField($d);
			}		
			$this->success("批量操作成功",url('index'));					
	}	
	

}